package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.entity.Slide;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.SlideMapper;
import com.api.syh56.shengyunhui.service.AdminSlideService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminSlideServiceImpl extends ServiceImpl<SlideMapper,Slide> implements AdminSlideService {
    @Autowired
    private SlideMapper mapper;

    @Override
    public void upload(Slide slide) {
        System.out.println(slide);
        int insert = mapper.insert(slide);
        if (insert != 1) {
            throw new BaseException("添加数据失败");
        }
    }

    @Override
    public void deleteById(Long id) {
        if (id == null) {
            throw new BaseException("id不存在");
        }
        mapper.deleteById(id);
    }

    @Override
    public void updateBy(Slide slide) {
        Slide selectById = mapper.selectById(slide.getId());
        if (selectById == null) {
            throw new BaseException("id不存在");
        }
        mapper.updateById(slide);
    }

    @Override
    public List<Slide> selectAll() {
        List<Slide> slides = mapper.selectList(null);
        return slides;
    }

    @Override
    public Page<Slide> selectBy(String picName,Integer pageNum, Integer pageSize) {
        QueryWrapper<Slide> wrapper = new QueryWrapper<>();
        Page<Slide> slidePage = null;
        Page<Slide> page = new Page<>(pageNum, pageSize);
        if (!picName.isEmpty()) {
            //模糊查询
            wrapper.lambda().like(StringUtils.isNotBlank(picName),Slide::getPicName,picName);
            slidePage = mapper.selectPage(page, wrapper);
        }else {
            slidePage = mapper.selectPage(page, wrapper);
        }
        return slidePage;
    }
}
